package net.csdn.business.webhook.config;

import lombok.Data;
import net.csdn.business.common.config.wrapper.ThreadPoolWrapper;
import net.csdn.business.common.service.CommonAsyncService;
import net.csdn.business.webhook.config.properties.WebHookThreadPoolProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName AsyncThreadPoolConfig
 * @Description webhook异步线程池配置
 * @Author zhangyl
 * @Date 2023/5/7 15:42
 */
@Data
@EnableAsync
@Configuration
@EnableConfigurationProperties(WebHookThreadPoolProperties.class)
public class WebHookThreadPoolConfig {

    @Autowired
    private WebHookThreadPoolProperties webHookThreadPoolProperties;

    public final static String NOTIFY_EXECUTOR_POOL_NAME = "notifyTaskExecutor";

    public final static String SERVICE_EXECUTOR_POOL_NAME = "serviceExecutor";


    /**
     * 通知线程池名前缀
     */
    private String notifyThreadNamePrefix = "notify-async-";

    /**
     * 业务线程池名前缀
     */
    private String serviceThreadNamePrefix = "service-async-";


    @Bean(NOTIFY_EXECUTOR_POOL_NAME)
    //@ConditionalOnMissingBean(ThreadPoolTaskExecutor.class)
    public ThreadPoolTaskExecutor taskNotifyExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolWrapper.CustomThreadPoolTaskExecutor();
        executor.setCorePoolSize(webHookThreadPoolProperties.getCoreSize());
        executor.setMaxPoolSize(webHookThreadPoolProperties.getMaxSize());
        executor.setQueueCapacity(webHookThreadPoolProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(webHookThreadPoolProperties.getKeepAlive());
        executor.setThreadNamePrefix(notifyThreadNamePrefix);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        // 默认初始化所有核心线程
        executor.getThreadPoolExecutor().prestartAllCoreThreads();
        return executor;
    }


    @Bean(SERVICE_EXECUTOR_POOL_NAME)
    //@ConditionalOnMissingBean(ThreadPoolTaskExecutor.class)
    public ThreadPoolTaskExecutor taskServicExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolWrapper.CustomThreadPoolTaskExecutor();
        executor.setCorePoolSize(webHookThreadPoolProperties.getCoreSize());
        executor.setMaxPoolSize(webHookThreadPoolProperties.getMaxSize());
        executor.setQueueCapacity(webHookThreadPoolProperties.getQueueCapacity());
        executor.setKeepAliveSeconds(webHookThreadPoolProperties.getKeepAlive());
        executor.setThreadNamePrefix(serviceThreadNamePrefix);
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 线程池对拒绝任务的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();
        // 默认初始化所有核心线程
        executor.getThreadPoolExecutor().prestartAllCoreThreads();
        return executor;
    }

   /* @Bean
    public CommonAsyncService commonAsyncService(ThreadPoolTaskExecutor taskExecutor) {
        return new CommonAsyncService(webHookThreadPoolProperties.getTimeOut(), taskExecutor);
    }*/
}